package com.lb.videoplay.fragment;

import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.lb.videoplay.R;
import com.lb.videoplay.adapter.VideoAdapter;
import com.lb.videoplay.model.VideoModel;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr.lb
 * 时间： 2018/11/15
 * 包名： com.lb.videoplay.activity
 * 描述： 精选 Fragment
 */
public class FeaturedFragment extends Fragment {

    private ListView mRcAssort;
    private VideoAdapter mVideoAdapter;
    private List<VideoModel> mVideoModels;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //通过参数中的布局填充获取对应布局
        View view = inflater.inflate(R.layout.fragment_featured, container, false);
        initWidget(view);
        initView();
        //监听back必须设置的
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        //然后在写这个监听器
        view.setOnKeyListener(backlistener);

        return view;
    }



    private View.OnKeyListener backlistener = new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                //这边判断,如果是back的按键被点击了   就自己拦截实现掉
                if (i == KeyEvent.KEYCODE_BACK) {
                    if ((GSYVideoManager.instance().getPlayPosition() >= 0)) {
                        if (GSYVideoManager.backFromWindowFull(getActivity())) {
                            return true;
                        }
                    }
                    return true;//表示处理了
                }
            }
            return false;
        }
    };

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!isVisibleToUser) {//视频切换停止
            if (GSYVideoManager.instance().getPlayPosition() >= 0) {
                GSYVideoManager.releaseAllVideos();
            }
        }
    }

    //初始化控件
    private void initWidget(View view) {
        mRcAssort = view.findViewById(R.id.rv_featured);
    }

    private void initView() {
        mVideoModels = new ArrayList<>();
        VideoModel videoMode4 = new VideoModel();
        videoMode4.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/4BEAAB012CB1F2277FBD9A1258194B0D16900DD0_size112_w578_h327.jpeg");
        videoMode4.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3981428-102-009-132337.mp4?vid=4101a5ad-750d-47ba-b042-2280ed7833de&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=美拍新鲜事&cat=172-176&ptype=172&platform=pc&sourceType=h5&dt=1540963358000&gid=A1_BUWd4JXH-&sign=d2d0270db9600bdefaa62c5d7aa6146e&tm=1543908979200");
        videoMode4.setType(4);
        mVideoModels.add(videoMode4);

        VideoModel videoModel = new VideoModel();
        videoModel.setPicUrl("http://e0.ifengimg.com/09/2018/1204/35CE98DCB0D6CE5CD335F443D24C69AAD53839CA_size133_w640_h360.jpeg");
        videoModel.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/12/04/p5581163-102-009-150737.mp4?vid=7ad9ccbf-6ca6-4443-88fb-b1bebe0ff299&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=快手&cat=172-176&ptype=172&platform=pc&sourceType=h5&dt=1543907192000&gid=HHygQWd4JfQT&sign=6e3a8a7b0a9bb91e9ee3f4d4962bdf21&tm=1543909012501");
        videoModel.setType(1);
        mVideoModels.add(videoModel);

        VideoModel videoMode2 = new VideoModel();
        videoMode2.setPicUrl("http://p0.ifengimg.com/pmop/2018/11/26/wf2_4926812_172009_sizeb_w1920_h1080.jpg");
        videoMode2.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/11/26/4926812-102-008-1720.mp4?vid=01eb8d00-04bc-4b5e-bd9d-29c50d36533b&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=凤凰网视频&cat=10155-10158&ptype=10155&platform=pc&sourceType=h5&dt=1543223880000&gid=oj2zsWd4UBSn&sign=ef1876e8bcaaa0e9fdb9b91c6fa7f828&tm=1543911773354");
        videoModel.setType(2);
        mVideoModels.add(videoMode2);

        VideoModel videoMode3 = new VideoModel();
        videoMode3.setPicUrl("http://p0.ifengimg.com/a/2018_06/12f5dbc585c998d.jpg");
        videoMode3.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/02/09/4785328-102-007-2105.mp4?vid=01d55717-5f89-46ed-81c9-591cb7371559&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=高端访谈&cat=10155-10156&ptype=10155&platform=pc&sourceType=h5&dt=1518181440000&gid=fuIGEWd4UN1b&sign=c081db3711d1fc44b8fb5312e0c7c2f6&tm=1543911824734");
        videoModel.setType(3);
        mVideoModels.add(videoMode3);

        VideoModel videoMode5 = new VideoModel();
        videoMode5.setPicUrl("http://p0.ifengimg.com/pmop/2018/10/24/wf2_4913167_133530_sizeb_w1920_h1080.jpg");
        videoMode5.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/24/4913167-102-008-1335.mp4?vid=01a630ae-b994-4372-9ccf-14040e153094&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=网罗天下&cat=10155-10157&ptype=10155&platform=pc&sourceType=h5&dt=1540358880000&gid=xovgkWd4U638&sign=f92220a17fdfc26d4b72ddec5a8e523b&tm=1543912009214");
        videoModel.setType(5);
        mVideoModels.add(videoMode5);

        VideoModel videoMode6 = new VideoModel();
        videoMode6.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/5CE46B8E7A7E7DFBBDE71DBF44835CC065ED3459_size829_w1620_h1080.jpeg");
        videoMode6.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3987021-102-009-144540.mp4?vid=a2fc523f-68a3-4877-bfc7-be3b2cc5f237&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=宫聆&cat=105-10057&ptype=105&platform=pc&sourceType=h5&dt=1540968060000&gid=Rts1kWd4YQpR&sign=d17bdff2062368241f2719825274bba4&tm=1543912884818");
        videoModel.setType(6);
        mVideoModels.add(videoMode6);

        VideoModel videoMode7 = new VideoModel();
        videoMode7.setPicUrl("http://e0.ifengimg.com/04/2018/1204/76554A7ED40A83E02CA44E6779304405C4681C9B_size315_w1325_h883.jpeg");
        videoMode7.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/12/04/p5584453-102-009-154738.mp4?vid=60ac609e-ddf6-4280-b883-4af4e758768c&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=胡椒视频&cat=105-107&ptype=105&platform=pc&sourceType=h5&dt=1543909575000&gid=bJ0J8Wd4YeLq&sign=60a57b3e9cc6855d1b3b1608dc23bdbb&tm=1543912940268");
        videoModel.setType(7);
        mVideoModels.add(videoMode7);


        VideoModel videoMode8 = new VideoModel();
        videoMode8.setPicUrl("http://d.ifengimg.com/q100/img1.ugc.ifeng.com/newugc/20181031/14/wemedia/944a0c3572cd5f59c7b02ef945cf4ea5815e395c_size284_w640_h360.png");
        videoMode8.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3988540-102-009-145537.mp4?vid=0c08d2ea-2c38-4eb0-aed4-d0d10505a5f9&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=咪眼看天下&cat=61-62&ptype=61&platform=pc&sourceType=h5&dt=1540968905000&gid=nWYXUWd4ZBvX&sign=5e9634d264335cfc172c2db7b887228e&tm=1543913085913");
        videoModel.setType(8);
        mVideoModels.add(videoMode8);

        VideoModel videoMode9 = new VideoModel();
        videoMode9.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/F0F8128D0737B4184789F04C23D0F184876EC4F2_size140_w580_h326.jpeg");
        videoMode9.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3992328-102-009-154538.mp4?vid=0bf40ea0-6621-4326-a340-34e7e310e717&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=我为汽车代言&cat=94-96&ptype=94&platform=pc&sourceType=h5&dt=1540971331000&gid=X0veIWd4lgWH&sign=80090d9f74723db573c3ad125212caf2&tm=1543916357001");
        videoModel.setType(9);
        mVideoModels.add(videoMode9);

        VideoModel videoMode10 = new VideoModel();
        videoMode10.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/8BBBF4969BE1BA82B9FD56743F50C9DA5DAF96DF_size330_w1074_h716.jpeg");
        videoMode10.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3992090-102-009-154138.mp4?vid=78ae4cdd-fa31-4253-a97d-b10a72240351&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=科普问天使者&cat=94-96&ptype=94&platform=pc&sourceType=h5&dt=1540971147000&gid=sBqWsWd4i-oE&sign=97c2f5b1b35a293d9f2b70ce1fdd3757&tm=1543915698694");
        videoModel.setType(10);
        mVideoModels.add(videoMode10);

        VideoModel videoMode11 = new VideoModel();
        videoMode11.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/E32AD265A38CB7990F7A5E03045EF227725A7798_size680_w1620_h1080.jpeg");
        videoMode11.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3992015-102-009-154038.mp4?vid=0328f709-ac4c-40ea-a45e-e32edbc7c219&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=印象历史&cat=94-97&ptype=94&platform=pc&sourceType=h5&dt=1540971097000&gid=fPwwUWd4jO6U&sign=f68d69c815053456abef8fe490fe67ee&tm=1543915761305");
        videoModel.setType(11);
        mVideoModels.add(videoMode11);

        VideoModel videoMode12 = new VideoModel();
        videoMode12.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/8A20F54848E8AF0AECE1231C8C327A729BBFDF7D_size348_w810_h540.jpeg");
        videoMode12.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3994580-102-009-161338.mp4?vid=77e07b85-e39a-44b3-b802-da3a3308d1e7&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=叫我王者&cat=172-175&ptype=172&platform=pc&sourceType=h5&dt=1540972749000&gid=xgCj4Wd4mlII&sign=8972d430124244ac16887eeef01f349a&tm=1543916638729");
        videoModel.setType(12);
        mVideoModels.add(videoMode12);

        VideoModel videoMode13 = new VideoModel();
        videoMode13.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/6A7E3FB3C6E85D7223BE0C84C77D9B5DC68AE3E8_size104_w580_h325.jpeg");
        videoMode13.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3991641-102-009-153526.mp4?vid=86aed2b8-e5df-43eb-96db-04275ed9c67b&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=嘻哈益食坊&cat=94-96&ptype=94&platform=pc&sourceType=h5&dt=1540970830000&gid=nk4FgWd4jfho&sign=a1d5fa3609a9179905e9aeee4d6c9399&tm=1543915829355");
        videoModel.setType(13);
        mVideoModels.add(videoMode13);

        VideoModel videoMode14 = new VideoModel();
        videoMode14.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/C2515D6FCDC157D88B4A92489B7B1B0D367DEC5B_size503_w1295_h863.jpeg");
        videoMode14.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3994632-102-009-161338.mp4?vid=3d15f7fd-3e9d-4480-ab18-7db1842cb56e&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=欧阳华北&cat=172-178&ptype=172&platform=pc&sourceType=h5&dt=1540972792000&gid=sQuEQWd4msQd&sign=100355b7be8e14b8bfd92f860d3bf107&tm=1543916667935");
        videoModel.setType(14);
        mVideoModels.add(videoMode14);

        VideoModel videoMode15 = new VideoModel();
        videoMode15.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/7034E8CD519D6D659FBF73B752DA80332D7C2677_size159_w580_h326.jpeg");
        videoMode15.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3994532-102-009-161237.mp4?vid=f7ce2289-c00b-43b7-aae4-96ec978911b7&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=网游那点事&cat=172-176&ptype=172&platform=pc&sourceType=h5&dt=1540972708000&gid=cz2kIWd4nA_Y&sign=ad8b318c76c8b8029c0906930d5070cf&tm=1543916752793");
        videoModel.setType(15);
        mVideoModels.add(videoMode15);

        VideoModel videoMode16 = new VideoModel();
        videoMode16.setPicUrl("http://p0.ifengimg.com/pmop/2018/1031/589D32F1C6E11F5B57C88A5850B10DCA30D2A4EE_size147_w580_h326.jpeg");
        videoMode16.setVideoUrl("http://ips.ifeng.com/video19.ifeng.com/video09/2018/10/31/p3994565-102-009-161240.mp4?vid=ab686646-6f6d-4bd1-a855-1006ce40d1ac&uid=1543908807562_vy3em97766&from=v_Free&pver=vHTML5Player_v2.0.0&sver=&se=主机游戏那点事&cat=172-176&ptype=172&platform=pc&sourceType=h5&dt=1540972740000&gid=ThDOIWd4nHGf&sign=8e6220595fd3ac94565c287003b15bc3&tm=1543916777889");
        videoModel.setType(16);
        mVideoModels.add(videoMode16);

        mVideoAdapter = new VideoAdapter(getActivity(), mVideoModels);

        mRcAssort.setAdapter(mVideoAdapter);
        mRcAssort.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int lastVisibleItem = firstVisibleItem + visibleItemCount;
                //大于0说明有播放
                if (GSYVideoManager.instance().getPlayPosition() >= 0) {
                    //当前播放的位置
                    int position = GSYVideoManager.instance().getPlayPosition();
                    //对应的播放列表TAG
                    if (GSYVideoManager.instance().getPlayTag().equals(VideoAdapter.TAG)
                            && (position < firstVisibleItem || position > lastVisibleItem)) {
                        if (GSYVideoManager.isFullState(getActivity())) {
                            return;
                        }
                        //如果滑出去了上面和下面就是否，和今日头条一样
                        GSYVideoManager.releaseAllVideos();
                        mVideoAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        GSYVideoManager.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        GSYVideoManager.releaseAllVideos();
        if (mVideoModels == null) {
            mVideoModels.clear();
            mVideoModels = null;
        }
    }
}
